/**
 * @(#) NoticeViewMediator.as
 *
 * @date		2014. 07. 1.
 * @version		1.0
 * @author		백광현
 * @description	연차사용통지서 프로세스 구현 nos
 * 				 
 */
package com.skt.common_hr.alnotice.view
{
	import com.skt.common_hr.alnotice.AlnoticeFacade;
	import com.skt.common_hr.alnotice.events.NoticeEvent;
	import com.skt.common_hr.alnotice.model.NoticeViewProxy;
	import com.skt.common_hr.alnotice.view.components.AlnoticeList;
	import com.skt.common_hr.common.puremvc.CommonFacade;
	import com.skt.common_hr.common.puremvc.OverViewMediator;
	
	import flash.events.Event;
	
	import mx.controls.Alert;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	

	public class NoticeOverViewMediator extends OverViewMediator
	{
		public static const NAME:String = "NoticeViewMediator";
		
		private var contentIdx:String;

		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function NoticeOverViewMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
		override protected function initializeView():void 
 		{
 			super.initializeView();
 		}
 		
 		override protected function activatedView():void
 		{
 			super.activatedView();
			view.vsNotice.selectedIndex = 0;
 
			loadNoticeList();
			setEventListeners();
			
//			 var date:Date= DateUtil.firstDateofThisMonth();
// 			view.BEGDA = new Date(date);
//
//			var date1:Date= DateUtil.lastDateofThisYear();
// 			view.ENDDA = new Date(date1);
 		}
 		
 		override protected function closedView():void
 		{
 			super.closedView();
 			removeEventListeners();
 		}
 		
		private function setEventListeners():void
		{
			view.addEventListener(AlnoticeList.NoticeAllData, loadNoticeList);
			view.addEventListener(AlnoticeList.NoticeDelete, onNoticeDelete);
			view.addEventListener(AlnoticeList.NoticeWrite, onNoticeWrite);
			view.addEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);

		} 		
 		
 		private function removeEventListeners():void
 		{
			view.removeEventListener(AlnoticeList.NoticeAllData, loadNoticeList);
			view.removeEventListener(AlnoticeList.NoticeDelete, onNoticeDelete);
			
			view.removeEventListener(AlnoticeList.NoticeWrite, onNoticeWrite);
			view.removeEventListener(NoticeEvent.SELECTED_ITEM, onDetailView);
			
 		}
 		
  		private function loadNoticeList(event:Event = null):void
 		{
			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.loadList();
			
			//버튼권한체크 
			//setAuthority1();
 		}
 		


		private function increaseViewCount(index:Number):void
		{
			var noticeProxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			noticeProxy.increaseCount(index);
		}
		
 		
 		
 		
 		
 		
 		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return basicInterestMessages.concat([
					AlnoticeFacade.LOAD_NOTICE_COMPLETE,
					AlnoticeFacade.MODE_LIST_NOTICE
					]);
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			super.handleNotification(note);
			switch(note.getName()) 
			{
				case AlnoticeFacade.LOAD_NOTICE_COMPLETE:
					displayNoticeList(note.getBody() as NoticeViewProxy);
					break;
				case AlnoticeFacade.MODE_LIST_NOTICE:
					view.vsNotice.selectedIndex = 0;
					if(Boolean(note.getBody())) {
						loadNoticeList();
					}
					break;
				default:
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}
		


		/**
		 * Proxy에서 DataLoad를 완료한 후 호출됨
		 *     - load된 데이터를 화면에 출력
		 * @param
		 * @return void
		 */
		 
		public var O_MEDCK:String='';
		public  var O_COMP:String="";
		public  var O_HOME:String="";
		
		public var reqErrMsg:String='';
		
 		private function displayNoticeList(proxy:NoticeViewProxy):void
 		{
 			reqErrMsg			= proxy.reqErrMsg	;
 			view.dataProvider 	= proxy.noticeList;
 			
// 			var objCbValWrap:Object={};
// 			objCbValWrap.O_MEDCK	= proxy.O_MEDCK	;
// 			objCbValWrap.O_FMEDCK	= proxy.O_FMEDCK	;
// 			view.noticeWrite.setComboVal(objCbValWrap);
 			
 			CommonFacade(facade).setStatusMessage("STATUS_COMPLETE_LOAD_NOTICE_LIST", [view.dataProvider.length]);
 		}
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event handler (from View, Internal or....)
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		private function onNoticeWrite(event:Event):void
 		{
			view.vsNotice.selectedIndex = 1; 			
 			facade.sendNotification(AlnoticeFacade.MODE_WRITE_NOTICE, this);			
			view.noticeWrite.dataProvider =null;
//			view.noticeWrite.sWriteType		= "init";
//			view.noticeWrite.hbWriteBox.visible	= true; 
 		}
 		
		//보기와 수정시  
		private function onDetailView(event:NoticeEvent):void
		{
			view.vsNotice.selectedIndex = 1;
			facade.sendNotification(AlnoticeFacade.MODE_MODIFY_NOTICE, event.selectedItem);
			return;
			
			
			/* 
			//view.vsNotice.selectedIndex = 1;
			view.vsNotice.selectedIndex = 2;			
			view.noticeWrite.dataProvider = event.selectedItem;
			view.noticeWrite.sWriteType		= "view";
			
			// 임시저장 상태라면 
			if(event.selectedItem.HLSTA == "1") {
				view.noticeWrite.hbWriteBox.visible	= true;
			}
			else {
				view.noticeWrite.hbWriteBox.visible	= false;
			}
			facade.sendNotification(AlnoticeFacade.MODE_WRITE_NOTICE, this);
			//view.noticeDetail.dataProvider = event.selectedItem;
			//increaseViewCount(event.selectedItem.CONTENT_IDX);.
			
			 */
		}
		
		
 		private function onNoticeDelete(event:Event):void
 		{
			var param:Object = new Object();
 			param.I_HENUM		= view.writeKey;
 			var proxy:NoticeViewProxy = facade.retrieveProxy(NoticeViewProxy.NAME) as NoticeViewProxy;
			proxy.deleteData(param);
 		}

		private function onNoticeDetailCancel(event:Event):void
		{
			view.vsNotice.selectedIndex = 0;
			loadNoticeList();
		}

 
 
 
 		private function clearWriteView():void
 		{
			view.noticeWrite.dataProvider = null;
 		}


		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():AlnoticeList
        {
            return viewComponent as AlnoticeList;
        }
	}
}